package com.version2_0.controller;

import com.alibaba.fastjson.JSONObject;
import com.version2_0.service.UserService2_0;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * Created by reeveslee on 16/6/18.
 */
@Controller
public class UserController2_0 {
    private static final Logger LOGGER = Logger.getLogger(UserController2_0.class);

    @Autowired
    private UserService2_0 userService20;

    /**
     * 用户获取token
     *
     * @return
     */
    @RequestMapping(value = "/v2.0/user/auth/token", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    @ResponseBody
    public String kbToken(@RequestBody JSONObject jsonObject) {
        try {
            int type = jsonObject.getIntValue("type"); // 1: 手机号登陆,0: 第三方登陆

            if (type == 0) {
                // 第三方登陆
                String platform = jsonObject.getString("platform");
                int os = jsonObject.getIntValue("os"); // 1: android, 0: iOS
                String openid = jsonObject.getString("openid");
                String token = jsonObject.getString("token");

                return userService20.kbToken(platform, os, openid, token);
            } else {
                String telnumber = jsonObject.getString("telnumber");
                String passwd = jsonObject.getString("password");

                return userService20.kbToken(telnumber, passwd);
            }

        } catch (Exception e) {
            LOGGER.error(e);
            return "{\"code\":400,\"info\":\"error request with no enough valid parameter\"}";
        }
    }
}
